package top.dingyingying.springboot3.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import top.dingyingying.springboot3.base.PageBean;
import top.dingyingying.springboot3.base.R;
import top.dingyingying.springboot3.dao.TbSoftwareTypeDAO;
import top.dingyingying.springboot3.entity.TbSoftwareType;
import top.dingyingying.springboot3.service.TbSoftwareTypeService;
import top.dingyingying.springboot3.vo.PageVo;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbSoftwareTypeServicelmpl implements TbSoftwareTypeService {
    private final TbSoftwareTypeDAO tbSoftwareTypeDAO;

    @Override
    public TbSoftwareType queryCname(String tname){
        QueryWrapper<TbSoftwareType> wrapper = new QueryWrapper<>();
        wrapper.eq("tname", tname);
        TbSoftwareType tbSoftwareType = tbSoftwareTypeDAO.selectOne(wrapper);
        return tbSoftwareType;
    }

    @Override
    public R<String> insert(TbSoftwareType tbSoftwareType){
        TbSoftwareType check = queryCname(tbSoftwareType.getTname());
        if (check != null){
            return R.fail("软件类型已经存在", null);
        }
        int i = tbSoftwareTypeDAO.insert(tbSoftwareType);
        return i == 1 ? R.ok("新增成功", null) : R.fail("新增失败", null);
    }
      @Override
      public R<PageVo<TbSoftwareType>> queryPage(PageBean pageBean, TbSoftwareType tbSoftwareType) {
      IPage<TbSoftwareType> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
      QueryWrapper<TbSoftwareType> wrapper = new QueryWrapper<>();
      wrapper.like(StringUtils.hasText(tbSoftwareType.getTname()), "tname", tbSoftwareType.getTname());
      page = tbSoftwareTypeDAO.selectPage(page, wrapper);
      return R.ok(page);

  }
    @Override
    public R<TbSoftwareType> queryById( Integer id) {
        TbSoftwareType tbSoftwareType = tbSoftwareTypeDAO.selectById(id);
        return tbSoftwareType == null ? R.fail("查询失败", null) : R.ok(tbSoftwareType);
    }

    @Override
    public R<String> update(TbSoftwareType tbSoftwareType){
        int i = tbSoftwareTypeDAO.updateById(tbSoftwareType);
        return i == 1 ? R.ok("修改成功", null) : R.fail("修改失败", null);
    }

@Override
public R<String> delete(Integer id){
    int i = tbSoftwareTypeDAO.deleteById(id);
    return i == 1 ? R.ok("删除成功", null) : R.fail("删除失败", null);
}

}
